<?php

namespace Frieza\Modules\Tiles\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Biz\model\tiles\TilesRecords;
use Biz\service\Tiles;
use Doraemon\service\User;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class HistoryController extends Controller
{
    const PAGE_SIZE = 20;


    public function history(Request $request, Content $content)
    {
        $pageSize = $request->input("per_page", 20);
        $page = $request->input("page", 1);
        if (!$request->input('record_time')) {
            $request->merge([
                'record_time' => [
                    'start' => date('Y-m-d', strtotime("-7days")),
                    'end'   => date('Y-m-d', strtotime('+1days')),
                ]
            ]);
        }
        $where = $this->_index_condition($request->all());
        $where['LIMIT'] = [
            ($page - 1) * $pageSize,
            $pageSize
        ];
        $where['ORDER'] = [
            'id' => 'DESC'
        ];
        $history = Tiles::admGetHistory($where);
        $gridModel = new pGridDataModel($history['list'], $history['total'], [
            'perPage' => $pageSize
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->disableCreateButton();
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableActions();
                $grid->paginate($gridModel->getPerPage());
                $grid->filter(function (Grid\Filter $filter) {
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->dropDown('user', [
                            'uid'  => '用户uid',
                            'nick' => '用户昵称',
                        ]);
                        $filter->equal('record_type', '交易类型')->select(TilesRecords::RECORD_TYPE_DESC);
                    });
                    $filter->column(1 / 2, function (Grid\Filter $filter) {
                        $filter->equal('transaction_id', '交易单号');
                        $filter->between('addtime', '交易时间')->datetime();

                    });
                    $filter->expand();
                });
                $grid->column('uid', '用户')->display(function () {
                    return '<p>' . $this->uid . '</p>' . '<p>' . $this->nick . '</p>';
                });
                $grid->column('record_type', '交易类型')->display(function () {
                    return TilesRecords::RECORD_TYPE_DESC[$this->record_type];
                });
                $grid->column('amount', '交易金额');
                $grid->column('transaction_id', '流水号');
                $grid->column('record_info', '交易信息');
                $grid->column('record_time', '交易时间');
            }
        );
        return $content->header('订单列表')->breadcrumb(
            ['text' => '订单列表'])
            ->row($gridList->render());
    }

    public function _index_condition($request)
    {
        $response = [];
        if (isset($request['transaction_id']) && strlen($request['transaction_id']) > 0) {
            $response['transaction_id'] = $request['transaction_id'];
            return $response;
        }
        if (isset($request['record_type']) && ($request['record_type'] > 0)) {
            $response['record_type'] = $request['record_type'];
        }
        if (isset($request['uid']) && $request['uid'] > 0) {
            $response['uid'] = $request['uid'];
        }
        if (isset($request['nick']) && strlen($request['nick']) > 0) {
            $uid = User::getUidByNick($request['nick']);
            $response['uid'] = $uid;
        }
        if (isset($request['record_time'])) {
            if (isset($request['record_time']['start']) && strlen($request['record_time']['start']) > 0) {
                $response['record_time[>=]'] = $request['record_time']['start'];
            }
            if (isset($request['record_time']['end']) && strlen($request['record_time']['end']) > 0) {
                $response['record_time[<=]'] = $request['record_time']['end'];
            }
        }
        return $response;
    }
}